<?php
/**
 +------------------------------------------------------------------------------
 * framework 入口文件
 +------------------------------------------------------------------------------
 * @Version 1.9
 * @Author  WangXian
 * @E-mail   admin@loopx.cn
 * @package ephp
 * @FileName index.php
 * @Creation date 2010-9-8
 * @Modified date 2010-10-11
 +------------------------------------------------------------------------------
 */
if(!defined('APP_PATH')) exit;

#系统运行效率标记
$run_start_time = microtime(1);			#启动时间
$run_start_memory = memory_get_usage();	#内存使用大小
$run_dbquery_count = 0;					#系统运行中，执行了多少次QUERY

#定义系统常量
define("URL", $_SERVER['SCRIPT_NAME']);	#带入口文件index.php,例如:/book/index.php

$source_dir = (($sdir = dirname($_SERVER['SCRIPT_NAME'])) == '/' || $sdir == '\\') ? '' : $sdir;
define("SOURCE", $source_dir.'/assets' );	 #项目路径

include FW_PATH.'/common/functions_base.php';#加载公共方法

#处理项目配制参数,进行预处理
if(file_exists(APP_PATH.'/runtime/cache/~config.php') && (! file_exists(APP_PATH.'/conf/main.config.php') || filemtime(APP_PATH.'/runtime/~config.php')>filemtime(APP_PATH.'/conf/main.config.php') ) )
	C('', 'main', include APP_PATH.'/runtime/cache/~config.php');
else
{
	C('', 'main', include FW_PATH.'/conf/convention.config.php');
	if(file_exists(APP_PATH.'/conf/main.config.php')) C('', 'main', include APP_PATH.'/conf/main.config.php');
	if(C('debug')) C('', 'main', include FW_PATH.'/conf/dev.config.php');
	else file_put_contents(APP_PATH.'/runtime/~config.php', '<?php return '.var_export(C(),true).';?>');
}

#获取main系统配置
$config = C();

#时区 & session 设置
date_default_timezone_set($config['date_timezone']);

#显示系统错误
if($config['debug'] == true)
{
	ini_set('display_errors', $config['display_errors']);
	error_reporting($config['error_reporting']);
}


#是否启用session, open_session = true的时候干预系统session的设置，否则采用php.ini默认设置
if ( $config['session']['open_session'] )
{
	#重设置session参数
	if( $config['session']['reset'])
	{
		#如果服务器设置auto_start = 1，则先注销当前session
		if( ini_get('session.auto_start') ) session_destroy();

		ini_set('session.name', $config['session']['name']);
		ini_set('session.use_cookies', $config['session']['use_cookies']);
		ini_set('session.use_only_cookies', $config['session']['use_only_cookies']);
		ini_set('session.use_trans_sid', $config['session']['use_trans_sid']);
		if(! empty($_GET[ $config['session']['name'] ])) session_id( $_GET[ $config['session']['name'] ] );
		session_start();
	}
	else if(! ini_get('session.auto_start') ) session_start();
}


#加载核心类
include FW_PATH.'/exception/ephpexception.class.php';
include FW_PATH.'/core/view.class.php';
include FW_PATH.'/core/app.class.php';
include FW_PATH.'/core/controller.class.php';
include FW_PATH.'/core/model.class.php';

#加载数据库驱动
if( empty($config['dbdriver']) ) $config['dbdriver'] = 'mysqli';
$driver_name = FW_PATH.'/db/'. $config['dbdriver'] .'.class.php';

if(file_exists($driver_name))
	include $driver_name;
else
	show_error('错误：配置文件main.config.php中使用了不支持的驱动 <strong><font color="#480091">'. $config['dbdriver'] .'</font></strong> 请使用：mysqli、mysql、sqlite 。',1013);

#校验runtime/logs/目录是否可写，避免wlog()方法抛出Fatal error: Uncaught exception 'ErrorException'
if(! is_writable(APP_PATH.'/runtime/logs/'))
	show_error('Log目录 <strong><font color="#480091">'.APP_PATH.'/runtime/logs/</font></strong> 不存在 或 没有写权限！');

#设置error\exception异常处理函数
set_error_handler("error_handler");

#记录系统访问日志
if($config['access_log'])
{
	$str="\nREQUEST_URL:http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n";
	$str.='REMOTE_ADDR:'.$_SERVER['REMOTE_ADDR']."\n";
	$str.='HTTP_USER_AGENT:'.empty($_SERVER['HTTP_USER_AGENT'])?'':$_SERVER['HTTP_USER_AGENT']."\n";
	$str.='REMOTE_ADDR:'.$_SERVER['REMOTE_ADDR']."\n";
	$str.='------------------------------';
	wlog('AccessLog-'.date('Y-m-d').'.log', '['. date('H:i:s') .']'.$str);
}

//加载其他文件
//...